package com.suep.game_chat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.suep.game_chat.dto.GameCharacterDTO;
import com.suep.game_chat.dto.UserProfileDTO;
import com.suep.game_chat.entity.GameCharacter;
import com.suep.game_chat.entity.User;
import com.suep.game_chat.entity.UserPrivacy;
import com.suep.game_chat.mapper.GameCharacterMapper;
import com.suep.game_chat.mapper.UserMapper;
import com.suep.game_chat.mapper.UserPrivacyMapper;
import com.suep.game_chat.service.UserProfileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Service
public class UserProfileServiceImpl implements UserProfileService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserPrivacyMapper userPrivacyMapper;

    @Autowired
    private GameCharacterMapper gameCharacterMapper;

    @Override
    @Transactional
    public User updateProfile(String userId, UserProfileDTO profileDTO) {
        User user = userMapper.selectById(userId);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }

        user.setAvatar(profileDTO.getAvatar());
        user.setNickname(profileDTO.getNickname());
        user.setGameUid(profileDTO.getGameUid());
        user.setUpdateTime(LocalDateTime.now());

        userMapper.updateById(user);
        return user;
    }

    @Override
    public User getProfile(String userId) {
        return userMapper.selectById(userId);
    }

    @Override
    public UserPrivacy getPrivacySettings(String userId) {
        UserPrivacy privacy = userPrivacyMapper.selectById(userId);
        if (privacy == null) {
            // 如果隐私设置不存在，创建默认设置
            privacy = new UserPrivacy();
            privacy.setUserId(userId);
            privacy.setShowProfile(true);
            privacy.setShowGameProgress(true);
            privacy.setShowCharacters(true);
            privacy.setShowOnlineStatus(true);
            privacy.setShowLastLoginTime(true);
            privacy.setCreateTime(LocalDateTime.now());
            privacy.setUpdateTime(LocalDateTime.now());
            userPrivacyMapper.insert(privacy);
        }
        return privacy;
    }

    @Override
    @Transactional
    public UserPrivacy updatePrivacySettings(String userId, UserProfileDTO profileDTO) {
        UserPrivacy privacy = getPrivacySettings(userId);
        
        privacy.setShowProfile(profileDTO.getShowProfile());
        privacy.setShowGameProgress(profileDTO.getShowGameProgress());
        privacy.setShowCharacters(profileDTO.getShowCharacters());
        privacy.setShowOnlineStatus(profileDTO.getShowOnlineStatus());
        privacy.setShowLastLoginTime(profileDTO.getShowLastLoginTime());
        privacy.setUpdateTime(LocalDateTime.now());

        userPrivacyMapper.updateById(privacy);
        return privacy;
    }

    @Override
    @Transactional
    public GameCharacter addGameCharacter(String userId, GameCharacterDTO characterDTO) {
        GameCharacter character = new GameCharacter();
        character.setCharacterId(UUID.randomUUID().toString().replace("-", ""));
        character.setUserId(userId);
        character.setCharacterName(characterDTO.getCharacterName());
        character.setCharacterLevel(characterDTO.getCharacterLevel());
        character.setCharacterRarity(characterDTO.getCharacterRarity());
        character.setCharacterElement(characterDTO.getCharacterElement());
        character.setCharacterPath(characterDTO.getCharacterPath());
        character.setCharacterAvatar(characterDTO.getCharacterAvatar());
        character.setCharacterConstellation(characterDTO.getCharacterConstellation());
        character.setCharacterEidolon(characterDTO.getCharacterEidolon());
        character.setCharacterSkillLevel(characterDTO.getCharacterSkillLevel());
        character.setObtainTime(LocalDateTime.now());
        character.setCreateTime(LocalDateTime.now());
        character.setUpdateTime(LocalDateTime.now());

        gameCharacterMapper.insert(character);
        return character;
    }

    @Override
    @Transactional
    public GameCharacter updateGameCharacter(String userId, String characterId, GameCharacterDTO characterDTO) {
        GameCharacter character = gameCharacterMapper.selectById(characterId);
        if (character == null || !character.getUserId().equals(userId)) {
            throw new RuntimeException("角色不存在或无权限修改");
        }

        character.setCharacterName(characterDTO.getCharacterName());
        character.setCharacterLevel(characterDTO.getCharacterLevel());
        character.setCharacterRarity(characterDTO.getCharacterRarity());
        character.setCharacterElement(characterDTO.getCharacterElement());
        character.setCharacterPath(characterDTO.getCharacterPath());
        character.setCharacterAvatar(characterDTO.getCharacterAvatar());
        character.setCharacterConstellation(characterDTO.getCharacterConstellation());
        character.setCharacterEidolon(characterDTO.getCharacterEidolon());
        character.setCharacterSkillLevel(characterDTO.getCharacterSkillLevel());
        character.setUpdateTime(LocalDateTime.now());

        gameCharacterMapper.updateById(character);
        return character;
    }

    @Override
    public List<GameCharacter> getUserCharacters(String userId) {
        QueryWrapper<GameCharacter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return gameCharacterMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional
    public void deleteGameCharacter(String userId, String characterId) {
        GameCharacter character = gameCharacterMapper.selectById(characterId);
        if (character == null || !character.getUserId().equals(userId)) {
            throw new RuntimeException("角色不存在或无权限删除");
        }
        gameCharacterMapper.deleteById(characterId);
    }
} 